package com.yf.catcafe.controller;

import com.yf.catcafe.pojo.dto.MessageInfoDto;
import com.yf.catcafe.pojo.enity.Message;
import com.yf.catcafe.pojo.enity.User;
import com.yf.catcafe.pojo.vo.MessageInfoVo;
import com.yf.catcafe.result.Result;
import com.yf.catcafe.service.MessageService;
import com.yf.catcafe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/catcafe")
@CrossOrigin("*")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    @GetMapping("/messages")
    public Result<List<MessageInfoVo>> getMessages(){
        List<Message> messages=messageService.getMessages();
        List<MessageInfoVo> messageInfoVos=messages.stream().map(message -> {
            MessageInfoVo vo=new MessageInfoVo();
            vo.setContent(message.getContent());
            vo.setTime(message.getCreateTime().toString());
            vo.setUsername(message.getUserName());
            vo.setNickname(message.getNickName());
            return vo;
        }).collect(Collectors.toList());
        return Result.success(messageInfoVos);
    }

    @PostMapping("/messages")
    public Result insertMessage(@RequestBody MessageInfoDto messageInfoDto){
        messageService.insertMessages(messageInfoDto);
        return Result.success("留言成功");
    }
}
